package com.yanqu.road.server.manager.systemgroup.newmonopolymkt;

import com.yanqu.road.dao.impl.nenmonopolymarket.NewMonopolyMarketWeekStateDataDaoImpl;
import com.yanqu.road.dao.impl.nenmonopolymarket.office.NewMonopolyMarketOfficeFinishDataDaoImpl;
import com.yanqu.road.dao.impl.nenmonopolymarket.resgrab.NewMonopolyMarketResGrabFinishDataDaoImpl;
import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.entity.systemgroup.common.SystemServerGroupWeekData;
import com.yanqu.road.entity.systemgroup.newmonopolymarket.NewMonopolyMarketWeekStateData;
import com.yanqu.road.entity.systemgroup.newmonopolymarket.config.NewMonopolyMarketResConfigData;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.pb.newmonopolymkt.NewMonopolyMarketProto;
import com.yanqu.road.server.TempCrossMgr;
import com.yanqu.road.server.manager.systemgroup.Cross2SystemWeekGroupMatchMgr;
import com.yanqu.road.server.manger.NewMonopolyMarketCommonMgr;

import java.util.Calendar;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;

public class CrossNewMonopolyMarketMgr extends TempCrossMgr {

    /**
     * 最大公告条数 （默认50 没走配置）
     */
    public final static int NOTICE_MAX_NUM = 50;

    public static final int MAX_MESSAGE_COUNT = 50;

    /**
     * 资源点待派遣中间状态，保护时间
     */
    public static int RES_GRAB_CHANGE_TEAM_PROTECT_TIME = 30;

    public static AtomicLong maxResGrabFinishId;

    public static AtomicLong maxOfficeFinishId;

    //weekId,groupId
    public final static Map<Integer, Map<Integer, CrossNewMonopolyMarketGroup>> marketMap = new ConcurrentHashMap<>();

    public static synchronized CrossNewMonopolyMarketGroup getMarket(int groupId){
        int weekId = GameConfig.getSystemThisWeekId();
        SystemServerGroupWeekData groupWeekData = Cross2SystemWeekGroupMatchMgr.getGroupByGroupId(weekId, eSystemId.NewMonopolyMarket.getValue(), groupId);
        if(groupWeekData == null){
            return null;
        }
        if(!marketMap.containsKey(weekId)){
            marketMap.put(weekId, new ConcurrentHashMap<>());
        }
        if(!marketMap.get(weekId).containsKey(groupId)){
            CrossNewMonopolyMarketGroup market = new CrossNewMonopolyMarketGroup(groupWeekData);
            market.loadData();
            marketMap.get(weekId).put(groupId, market);
        }
        return marketMap.get(weekId).get(groupId);
    }

    public static Map<Integer, Map<Integer, CrossNewMonopolyMarketGroup>> getMarketMap() {
        return marketMap;
    }

    /**
     * 榷场 系统 是否开启
     * @param type
     * @return
     */
    public static boolean isSystemOpen(int type){
          return NewMonopolyMarketCommonMgr.isSystemOpenTime(type);
    }

    @Override
    public boolean removeExpireData() throws Exception {
        Calendar calendar = Calendar.getInstance();
        if(calendar.get(Calendar.DAY_OF_WEEK) == Calendar.THURSDAY){
            //周四清除上周记录
            int preWeekId = GameConfig.getSystemPreWeekId();
            if(marketMap.containsKey(preWeekId)){
                getLogger().info("remove preWeekId {} data.", preWeekId);
                marketMap.remove(preWeekId);
            }
        }
        return true;
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {

        int weekId = GameConfig.getSystemThisWeekId();

        //get weekData
        List<NewMonopolyMarketWeekStateData> list = new NewMonopolyMarketWeekStateDataDaoImpl().getNewMonopolyMarketWeekStateData(weekId);

        maxOfficeFinishId = new AtomicLong(new NewMonopolyMarketOfficeFinishDataDaoImpl().getMaxId());
        maxResGrabFinishId = new AtomicLong(new NewMonopolyMarketResGrabFinishDataDaoImpl().getMaxRecordId());

        if(!marketMap.containsKey(weekId)){
            marketMap.put(weekId, new ConcurrentHashMap<>());
        }

        //加载数据
        for (NewMonopolyMarketWeekStateData weekStateData : list) {
            CrossNewMonopolyMarketGroup group = new CrossNewMonopolyMarketGroup(weekStateData);
            group.loadData();
            marketMap.get(weekId).put(weekStateData.getSystemGroupId(), group);
        }

        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }


    @Override
    public boolean save() {

        for (Map.Entry<Integer, Map<Integer, CrossNewMonopolyMarketGroup>> entry : marketMap.entrySet()) {
            for (Map.Entry<Integer, CrossNewMonopolyMarketGroup> groupEntry : entry.getValue().entrySet()) {
                groupEntry.getValue().saveData();
            }
        }

        return true;
    }

    //pb
    public static NewMonopolyMarketProto.NewMonopolyMarketResConfigEntity.Builder builderMonopolyMarketResConfigEntity(NewMonopolyMarketResConfigData item){
        NewMonopolyMarketProto.NewMonopolyMarketResConfigEntity.Builder builder = NewMonopolyMarketProto.NewMonopolyMarketResConfigEntity.newBuilder();
        builder.setId(item.getId());
        builder.setLevel(item.getLevel());
        builder.setType(item.getType());
        builder.setReward(item.getReward());
        builder.setRewardAdd(item.getRewardAdd());
        builder.setLimit(item.getLimit());
        builder.setCost(item.getCost());
        builder.setMonster(item.getMonster());
        builder.setSeize(item.getSeize());
        builder.setPic(item.getPic());
        builder.setReturncost(item.getReturncost());
        builder.setNpcReturncost(item.getNpcReturncost());
        builder.setNpcList(item.getNpcList());
        builder.setAttackReward(item.getAttackReward());
        return builder;
    }


}
